package com.kmis.common.security;

import com.kmis.common.exception.UserCountLockException;
import com.kmis.entities.LoginUser;
import com.kmis.services.IUserService;
import com.kmis.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author moc
 * 自定义认证逻辑
 */
@Service
public class MyUserDetailServiceImpl implements UserDetailsService {

    private final IUserService userService;

    @Autowired
    public MyUserDetailServiceImpl(IUserService userService1) {
        this.userService = userService1;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserVo user = userService.getByUserName(username);
        if(Objects.isNull(user)) {
            throw new UsernameNotFoundException("用户名或者密码错误！");
        }else if(user.getStatus()==0){
            throw new UserCountLockException("该用户账号已被封禁，具体联系管理员！");
        }
        return new LoginUser(user);
    }
    }
